﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Table;

namespace chatblog
{
    public partial class blogsearch : System.Web.UI.Page
    {
        string userid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                dealrequest();
                bind1();
                bind2();
                //bind3();
            }
        }
        /// <summary>
        /// 绑定分类
        /// </summary>
        private void bind1()
        {
            cbusercata ct = cbusercata.Find(" cblayer = 1 and cbuserid = '" + userid + "'");
            GridView_cata.DataSource = ct.MyTable;
            GridView_cata.DataBind();
        }
        /// <summary>
        /// 处理request
        /// </summary>
        private void dealrequest()
        {
            if (this.Request.Params["keyword"] != null)
            {
                TextBox_keyword.Text = this.Request.Params["keyword"].ToString();
            }
            if (this.Request.Params["userid"] == null)
            {
                if (AliApi.appUserId != "")
                {
                    userid = AliApi.appUserId;
                }
                else
                {
                    this.Response.Redirect(ResolveUrl("~/blog/default.aspx"));
                }
                HyperLink_default.NavigateUrl = ResolveUrl("~/blog.aspx");
                cbuser u = cbuser.Find(" recid = " + AliApi.appDomainID);
                if (u.blogtitle.ToString() != "")
                {
                    HyperLink_default.Text = u.blogtitle.ToString();
                    Literal_desc.Text = u.blogdesc.ToString();
                }
                else
                {
                    HyperLink_default.Text = u.cbusernickname.ToString() + "的博客首页";
                }
            }
            else
            {
                cbuser u = cbuser.Find(" recid = " + this.Request.Params["userid"].ToString());
                userid = u.cbuserid.ToString();
                HyperLink_default.NavigateUrl = ResolveUrl("~/blog/") + this.Request.Params["userid"].ToString()+".html";
                HyperLink_addfriend.NavigateUrl = ResolveUrl("~/bbs/addfriend.aspx?recid=") + u.RECID.ToString();
                if (u.blogtitle.ToString() != "")
                {
                    HyperLink_default.Text = u.blogtitle.ToString();
                    Literal_desc.Text = u.blogdesc.ToString();
                }
                else
                {
                    HyperLink_default.Text = u.cbusernickname.ToString() + "的博客首页";
                }
            }
            this.Title = HyperLink_default.Text ;
        }
        static public string getstr()
        {
            string keyword = "";
            if (HttpContext.Current.Request.Params["keyword"] != null)
            {
                keyword = HttpContext.Current.Request.Params["keyword"].ToString();
            }
            if (AliApi.appDomainID != "" && HttpContext.Current.Request.Params["userid"] == null)
            {
                HttpContext.Current.Response.Redirect("blogsearch.aspx?userid=" + AliApi.appDomainID
                    + "&keyword=" + keyword);
            }
            if (HttpContext.Current.Request.Params["userid"] == null)
            {
                return "";
            }
            return "&userid=" + HttpContext.Current.Request.Params["userid"].ToString()
                + "&keyword=" + keyword;
        }
        /// <summary>
        /// 绑定正文列表
        /// </summary>
        private void bind2()
        {
            string sql = "";
            sql = " select top 100 * from vcblist where cblisttype = '发帖'  ";
            string keyword = zfunction.ReplaceKeyWord(TextBox_keyword.Text);
            if (this.Request.Params["cbuserid"] != null)
            {
                sql += " and cbuserid = '" + this.Request.Params["cbuserid"].ToString() + "' ";
            }
            if (this.Request.Params["cbusercataid"] != null)
            {
                sql += " and cbusercataid = '" + this.Request.Params["cbusercataid"].ToString() + "' ";
            }
            sql += " and (  cbtitle like '" + keyword + "' ";
            sql += " or cbcontent like '" + keyword + "' ";
            sql += " )";
            sql += "  order by jccreatedate desc ,jccreatetime desc ";
            DataTable dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            WebPager_list.DataSource = dt;
            WebPager_list.DataBind();
        }
        protected void WebPager_list_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            bind2();
            this.WebPager_list.CurrentPageIndex = e.NewPageIndex;
        }
    }
}
